package com.jic.manager.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.MerchantChannel;
import com.jic.manager.mapper.MerchantChannelMapper;
import com.jic.manager.request.MerchantChannelRequest;
import com.jic.manager.service.MerchantChannelService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import com.jic.util.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 商家渠道关联表
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:39:43
 * @version : v1.0.0
 */
@Service
@Slf4j
public class MerchantChannelServiceImpl implements MerchantChannelService{

    @Resource
    private MerchantChannelMapper merchantChannelMapper;

    @Override
    public long insert(MerchantChannel merchantChannel) {
        long flag = merchantChannelMapper.insert(merchantChannel);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return merchantChannelMapper.delete(id);
    }

    @Override
    public long update(MerchantChannel merchantChannel) {
        return merchantChannelMapper.update(merchantChannel);
    }

    @Override
    public MerchantChannel queryByPrimaryKey(Long id){
        return merchantChannelMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MerchantChannel> query(MerchantChannel merchantChannel){
        return merchantChannelMapper.query(merchantChannel);
    }

    @Override
    public PageResult<MerchantChannel> queryPage(MerchantChannel merchantChannel, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MerchantChannel> list = merchantChannelMapper.queryPage(startIndex, pageQuery.getLimit(), merchantChannel);
        long count = merchantChannelMapper.queryPageCount(merchantChannel);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> editMerchantChannelInfo(MerchantChannelRequest merchantChannel) {
        try{
            MerchantChannel merchantChannelVo = new MerchantChannel();
            BeanUtils.copyProperties(merchantChannel,merchantChannelVo);
            Long id = merchantChannel.getId();
            if (id != null && id !=0){
                merchantChannelMapper.update(merchantChannelVo);
            }else {
                //TODO 创建人
                merchantChannelVo.setCreateTime(new Date());
                merchantChannelVo.setDeleteFlag(0);
                String [] channelids=merchantChannel.getChannelIds().split(",");
                for(int i=0;i<channelids.length;i++){
                    merchantChannelVo.setChannelId(Long.parseLong(channelids[i]));
                    merchantChannelMapper.insert(merchantChannelVo);
                }
            }
            return RestResult.success("成功");
        }catch (Exception e){
            log.error("error",e);
           return RestResult.error("9999",e.getLocalizedMessage());
        }
    }
}